/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution
{
public:
    ListNode *oddEvenList(ListNode *head)
    {
        if (head == nullptr || head->next == nullptr)
        {
            return head;
        }
        ListNode *oddHead = head;
        ListNode *evenHead = head->next;
        ListNode *oddCur = oddHead;
        ListNode *evenCur = evenHead;
        ListNode *next = head->next->next;
        bool nextIsOdd = true;
        while (next != nullptr)
        {
            if (nextIsOdd)
            {
                oddCur->next = next;
                oddCur = next;
            }
            else
            {
                evenCur->next = next;
                evenCur = next;
            }
            next = next->next;
            nextIsOdd = !nextIsOdd;
        }
        oddCur->next = evenHead;
        evenCur->next = nullptr;
        return oddHead;
    }
};